<template>
    <div class="cart-container">
        
        <div class="cart-items" v-if="mycollection.length>0">
            <div class="goods-card" v-for="item in mycollection" :key="item.id">
                <div class="card-content">

                     <!-- 左边图片区域 -->
                     <div class="left">
                        <div class="img-container">
                            <img :src="item.img" :alt="item.name" class="product-img">
                        </div>
                    </div>
                    <!-- 右边信息区域 -->
                    <div class="right">
                        <div class="product-info">
                            <h3 class="product-name">{{item.name}}</h3>
                            <p class="product-desc">{{item.describe}}</p>
                            <div class="price-quantity">
                                <span class="price">¥{{item.price}}</span>
                                <div class="quantity-control">
                                    <label>数量：</label>
                                    <input 
                                        type="number" 
                                        v-model="item.num" 
                                        class="quantity-input" 
                                        min="1"
                                        @change="updateQuantity(item)"
                                    />
                                </div>
                            </div>
                        </div>
                        <div class="action-buttons">
                            <button class="btn-remove" @click="removeItem(item)">删除</button>
                            <button class="btn-select" :class="{'selected':item.selected}" @click="toggleSelect(item)">
                                {{item.selected ? '取消选中' : '选中购买'}}
                            </button>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <!-- 空购物车状态 -->
        <div class="empty-cart" v-else>
            <div class="empty-icon">🛒</div>
            <h3>购物车是空的</h3>
            <p>快去添加一些商品吧！</p>
        </div>

        <!-- 底部结算区域 -->
        <div class="checkout-section" v-if="mycollection.length > 0">
            <div class="checkout-info">
                <div class="total-info">
                    <span class="total-label">合计：</span>
                    <span class="total-price">{{sumPrice}}</span>
                </div>
                <div class="total-info">
                    <span class="total-label">商品数：</span>
                    <span class="total-quantity">{{sumNum}}</span>
                </div>
            </div>
            <button type="button" 
            @click="checkin()" 
            class="btn-checkout"
            :disabled="selectedCount===0">
                结算({{selectedCount}})
            </button>
        </div>

    </div>
</template>
<script>
import { computed } from 'vue';
import { useStore } from 'vuex';

export default{
    name:'mycollection',
    data(){
        return{
            mycollection:[]
        }
    },
    mygetdata(){//读取购物车中的所有商品
        let myparams = {status: '未提交',createBy: this.$store.state.loginName};
				this.getdata('/system/cart/list?pageNum=1&pageSize=42', myparams).then(					response => {
					if (response.code == 401) //认证失败，无法访问系统资源,跳转到登录页面
					{
						this.$router.push({name: 'login'});
						return;
					}				 
					this.cartlist.splice(0,this.cartlist.length);
					for (var index in response.rows) {
						let newmovies= {
							id: response.rows[index],
							img: "/public/a1.jpg",
							name: "测试",
							describe: "新产品",
							price: 999999,
							num: 0,
							kind: '',
							memo: ''
						};
						response.rows[index].img = response.rows[index].img;
						newmovies.id = response.rows[index].id;
						newmovies.img = response.rows[index].img;
						newmovies.name = response.rows[index].title;
						newmovies.describe = response.rows[index].mydescribe;
						newmovies.price = response.rows[index].price;
						newmovies.num = response.rows[index].num;
						newmovies.memo = response.rows[index].memo;
						this.mycollection.push(newmovies);
					}
				}).catch((err) => {})
			},		
    setup(){
        const store = useStore();
        
        // 使用Vuex获取购物车数据
        const mycollection = computed(() => store.getters.cartItems);
        
        return{mycollection}
    },
    computed:{
        // 使用Vuex getters获取计算属性
        sumNum(){
            return this.$store.getters.selectedItemTotal;
        },
        sumPrice(){
            return this.$store.getters.cartTotalPrice;
        },
        selectedCount(){
            return this.$store.getters.selectedItemCount;
        }
    },
    methods:{
        // 选中和取消选中
        toggleSelect(item){
            this.$store.dispatch('toggleCartItem', item.id);
        },
        
        // 更新数量
        updateQuantity(item){
            if(item.num<1){
                item.num=1;
            }
            this.$store.dispatch('updateCartQuantity', { id: item.id, num: item.num });
        },
        
        // 结算函数
        async checkin(){
            try {
                const result = await this.$store.dispatch('checkout');
                // 确认结算
                if (confirm(`确认结算 ${result.totalQuantity} 件商品，总价 ¥${result.totalPrice}？`)) {
                    // 这里可以发送选中商品到后端，或跳转支付页
                    alert(`购买成功！共 ${result.totalQuantity} 件商品，总价 ¥${result.totalPrice}`);
                }
            } catch (error) {
                alert(error.message);
            }
        },
        
        // 移除商品
        removeItem(item){
            if(confirm('确定要删除这件商品吗？')){
                this.$store.dispatch('removeFromCart', item.id);
            }
        }
    }
}

</script>
<style scoped>
/* 购物车容器 */
.cart-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0;
    background: transparent;
    min-height: auto;
}


/* 购物车商品列表 */
.cart-items {
    margin-bottom: 30px;
    display: grid;
    gap: 20px;
}

.goods-card {
    background: white;
    border-radius: 15px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    margin-bottom: 20px;
    overflow: hidden;
    transition: all 0.3s ease;
}

.goods-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.12);
}

.card-content {
    display: flex;
    padding: 20px;
}

/* 左侧图片区域 */
.left {
    width: 120px;
    height: 120px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-right: 20px;
}

.img-container {
    width: 100px;
    height: 100px;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.product-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* 右侧商品信息 */
.right {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.product-info {
    margin-bottom: 15px;
}

.product-name {
    font-size: 20px;
    font-weight: 600;
    color: #333;
    margin: 0 0 8px 0;
    line-height: 1.3;
}

.product-desc {
    font-size: 14px;
    color: #666;
    margin: 0 0 15px 0;
    line-height: 1.4;
}

.price-quantity {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.price {
    font-size: 24px;
    font-weight: 700;
    color: #e74c3c;
}

.quantity-control {
    display: flex;
    align-items: center;
    gap: 10px;
}

.quantity-control label {
    font-size: 14px;
    color: #666;
}

.quantity-input {
    width: 80px;
    padding: 8px 12px;
    border: 2px solid #e1e8ed;
    border-radius: 8px;
    text-align: center;
    font-size: 16px;
    transition: border-color 0.3s ease;
}

.quantity-input:focus {
    outline: none;
    border-color: #667eea;
}

/* 操作按钮 */
.action-buttons {
    display: flex;
    gap: 15px;
}

.btn-remove, .btn-select {
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.3s ease;
}

.btn-remove {
    background-color: #ff6b6b;
    color: white;
}

.btn-remove:hover {
    background-color: #ff5252;
    transform: translateY(-1px);
}

.btn-select {
    background-color: #f8f9fa;
    color: #667eea;
    border: 2px solid #667eea;
}

.btn-select:hover {
    background-color: #667eea;
    color: white;
}

.btn-select.selected {
    background-color: #667eea;
    color: white;
}

/* 空购物车状态 */
.empty-cart {
    text-align: center;
    padding: 80px 20px;
    background: white;
    border-radius: 15px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
}

.empty-icon {
    font-size: 80px;
    margin-bottom: 20px;
}

.empty-cart h3 {
    font-size: 24px;
    color: #333;
    margin: 0 0 10px 0;
}

.empty-cart p {
    font-size: 16px;
    color: #666;
    margin: 0;
}

/* 结算区域 */
.checkout-section {
    position: sticky;
    bottom: 20px;
    background: white;
    border-radius: 20px;
    padding: 25px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.15);
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 30px;
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.2);
}

.checkout-info {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.total-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-width: 200px;
}

.total-label {
    font-size: 16px;
    color: #666;
}

.total-price {
    font-size: 24px;
    font-weight: 700;
    color: #e74c3c;
}

.total-quantity {
    font-size: 18px;
    font-weight: 600;
    color: #333;
}

.btn-checkout {
    padding: 15px 40px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 18px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    min-width: 150px;
}

.btn-checkout:hover:not(:disabled) {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

.btn-checkout:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* 响应式设计 */
@media (max-width: 768px) {
    .cart-container {
        padding: 0;
    }
    
    .card-content {
        flex-direction: column;
        padding: 15px;
    }
    
    .left {
        width: 100%;
        height: auto;
        margin-right: 0;
        margin-bottom: 15px;
    }
    
    .img-container {
        width: 80px;
        height: 80px;
        margin: 0 auto;
    }
    
    .price-quantity {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }
    
    .action-buttons {
        flex-direction: column;
        gap: 10px;
    }
    
    .checkout-section {
        flex-direction: column;
        gap: 20px;
        text-align: center;
        position: relative;
        bottom: auto;
        margin-top: 20px;
    }
    
    .checkout-info {
        width: 100%;
    }
    
    .total-info {
        justify-content: space-between;
        width: 100%;
    }
}

@media (max-width: 480px) {
    .product-name {
        font-size: 18px;
    }
    
    .price {
        font-size: 20px;
    }
    
    .btn-checkout {
        width: 100%;
        padding: 12px 20px;
        font-size: 16px;
    }
    
    .checkout-section {
        padding: 20px;
        border-radius: 15px;
    }
}
</style>
